package com.jt9003.controller;


import com.jt9003.pojo.User1;
import com.jt9003.pojo.User_type;
import com.jt9003.service.User1Service;
import com.jt9003.service.User_typeService;
import com.jt9003.utils.PageUtils;
import com.jt9003.utils.RedisUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class User1Controller {

    @Resource
    private User1Service user1Service;
    @Resource
    private User_typeService user_typeService;
    @Resource
    private RedisUtil redisUtil;

    @RequestMapping("/insertUser")
    public Map<String, Object> insertUser(User1 user1) {
        Map<String, Object> map = new HashMap();
        System.out.println("user1=" + user1);
        int i = user1Service.insertUser(user1);
        if (i > 0) {
            map.put("msg", true);
            map.put("data", user1);
        } else {
            map.put("msg", false);
        }
        return map;
    }

    @RequestMapping("/deleteUserById")
    public Map<String, Object> deleteUserById(int uId) {
        Map<String, Object> map = new HashMap();
        int i = user1Service.deleteUserById(uId);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/deleteUsersById")
    public Map<String, Object> deleteUsersById(String checkList) {
        Map<String, Object> map = new HashMap();
        System.out.println("==>" + checkList);

        String[] strs = checkList.split(",");
        System.out.println(strs[0]);
        List<Integer> ids = new ArrayList<>();
        for (String str : strs) {
            ids.add(Integer.parseInt(str));

        }
        int i = user1Service.deleteUsersById(ids);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/updateUserById")
    public Map<String, Object> updateUserById(User1 user1) {
        Map<String, Object> map = new HashMap();
        int i = user1Service.updateUserById(user1);
        if (i > 0) {
            map.put("msg", true);
            map.put("data", user1);
        } else {
            map.put("msg", false);
        }
        System.out.println("map=" + map);
        return map;
    }

    @RequestMapping("/updateUserStateById")
    public Map<String, Object> updateUserStateById(User1 user1) {
        Map<String, Object> map = new HashMap();
        int i = user1Service.updateUserStateById(user1);
        if (i > 0) {
            map.put("msg", true);
            map.put("code", "0");
            map.put("data", user1);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/updateUserQuitDateByState")
    public Map<String, Object> updateUserQuitDateByState(User1 user1) {
        Map<String, Object> map = new HashMap();
        int i = user1Service.updateUserQuitDateByState(user1);
        if (i > 0) {
            map.put("msg", true);
            map.put("code", "0");
            map.put("data", user1);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/updateUserQuitDateById")
    public Map<String, Object> updateUserQuitDateById(User1 user1) {
        Map<String, Object> map = new HashMap();
        int i = user1Service.updateUserQuitDateById(user1);
        if (i > 0) {
            map.put("msg", true);
            map.put("code", "0");
            map.put("data", user1);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/updateUserAuthorityById")
    public Map<String, Object> updateUserAuthorityById(User1 user1) {
        Map<String, Object> map = new HashMap();
        int i = user1Service.updateUserAuthorityById(user1);
        if (i > 0) {
            map.put("msg", true);
            map.put("code", "0");
            map.put("data", user1);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectUserByLoginNameAndPwd")
    public Map<String, Object> selectUserByLoginNameAndPwd(String uLoginName, String uPassword) {
        Map<String, Object> map = new HashMap();
        User1 i = user1Service.selectUserByLoginNameAndPwd(uLoginName, uPassword);

        String userName = i.getuLoginName();    //获取登陆名
        String token = "LoggedInUsers:"+userName;
        boolean flag = redisUtil.set(token, i,86400);
        Object object = redisUtil.get(token);

        if (i != null) {
            map.put("msg", true);
            map.put("code", "0");
            map.put("data", i);
            map.put("token",token);
            map.put("redis", flag);
            map.put("object", object);
        } else {
            map.put("msg", false);
        }
        return map;
    }

    @RequestMapping("/selectUserByNameOrPinyinInitials")
    public Map<String, Object> selectUserByNameOrPinyinInitials(String uLoginName,
                                                                @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = user1Service.getUsersCountByResult(uLoginName);
        List<User1> list = user1Service.selectUserByNameOrPinyinInitials(uLoginName, start, limit);
        if (list != null) {
            map.put("msg", "success");
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getUsersCountByResult")
    public Map<String, Object> getUsersCountByResult(String uLoginName) {
        Map<String, Object> map = new HashMap();
        int i = user1Service.getUsersCountByResult(uLoginName);
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectUsersList")
    public Map<String, Object> selectUsersList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                               @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = user1Service.getUsersCount();
        List<User1> list = user1Service.selectUsersList(start, limit);
        if (list.size() > 0) {
            System.out.println("================start:" + start);
            System.out.println(list.get(0).gettId());
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).gettId() == 1) {
                    list.get(i).settIdShow("前台");
                } else if (list.get(i).gettId() == 2) {
                    list.get(i).settIdShow("医生");
                } else if (list.get(i).gettId() == 3) {
                    list.get(i).settIdShow("收银");
                } else if (list.get(i).gettId() == 4) {
                    list.get(i).settIdShow("药房管理");
                } else if (list.get(i).gettId() == 5) {
                    list.get(i).settIdShow("库房管理");
                } else if (list.get(i).gettId() == 6) {
                    list.get(i).settIdShow("超级管理员");
                }

                if (list.get(i).getDepartId() == 20) {
                    list.get(i).setDepartIdShow("门诊部");
                } else if (list.get(i).getDepartId() == 21) {
                    list.get(i).setDepartIdShow("行政部");
                } else if (list.get(i).getDepartId() == 22) {
                    list.get(i).setDepartIdShow("库房部");
                } else if (list.get(i).getDepartId() == 23) {
                    list.get(i).setDepartIdShow("药房部");
                } else if (list.get(i).getDepartId() == 24) {
                    list.get(i).setDepartIdShow("财务部");
                }

                if (list.get(i).getOfficesId() == 1) {
                    list.get(i).setOfficesIdShow("急诊科");
                } else if (list.get(i).getOfficesId() == 2) {
                    list.get(i).setOfficesIdShow("儿科");
                } else if (list.get(i).getOfficesId() == 3) {
                    list.get(i).setOfficesIdShow("五官科");
                } else if (list.get(i).getOfficesId() == 4) {
                    list.get(i).setOfficesIdShow("内科");
                } else if (list.get(i).getOfficesId() == 5) {
                    list.get(i).setOfficesIdShow("外科");
                }
            }
            map.put("msg", true);
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }


    @RequestMapping("/getUsersCount")
    public Map<String, Object> getUsersCount() {
        Map<String, Object> map = new HashMap();
        int i = user1Service.getUsersCount();
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }


    @RequestMapping("/selectUserAuthorityById")
    public Map<String, Object> selectUserAuthorityById(int uId) {
        Map<String, Object> map = new HashMap();
        User1 i = user1Service.selectUserAuthorityById(uId);
        System.out.println(i.getuArchives() + "--" + i.getuRegister() + "--" + i.getuStorageRoom() + "--" + i.getuDoctorWS() + "--" + i.getuPay() + "--" + i.getuPharmacy());
        if (i != null) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectUserAuthorityByuLoginName")
    public Map<String, Object> selectUserAuthorityByuLoginName(String uLoginName) {
        Map<String, Object> map = new HashMap();
        User1 i = user1Service.selectUserAuthorityByuLoginName(uLoginName);

        if (i != null) {
            map.put("status", "200");
            map.put("msg", true);
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectUserById")
    public Map<String, Object> selectUserById(int uId, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                              @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        User1 user = user1Service.selectUserById(uId);
        int i = user_typeService.getUserTypeCount();
        int start = PageUtils.getStart(page, limit);
        List<User_type> list = user_typeService.selectUserTypeList(start, limit);
        if (user.gettId() == 1) {
            user.settIdShow("前台");
        } else if (user.gettId() == 2) {
            user.settIdShow("医生");
        } else if (user.gettId() == 3) {
            user.settIdShow("收银");
        } else if (user.gettId() == 4) {
            user.settIdShow("药房管理");
        } else if (user.gettId() == 5) {
            user.settIdShow("库房管理");
        } else if (user.gettId() == 6) {
            user.settIdShow("超级管理员");
        }
        System.out.println("user:" + user.gettId() + "--" + user.gettIdShow());
        System.out.println(list);
        if (user != null) {
            map.put("status", "200");
            map.put("msg", true);
            map.put("userData", user);
            map.put("data", list);
            map.put("count", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }


    //根据用户名查询用户信息
    @RequestMapping("/selectUserByuLoginName")
    public Map<String, Object> selectUserByuLoginName(String uLoginName) {
        Map<String, Object> map = new HashMap();
        User1 i = user1Service.selectUserByuLoginName(uLoginName);

        if (i.getDepartId() == 20) {
            i.setDepartIdShow("门诊部");
        } else if (i.getDepartId() == 21) {
            i.setDepartIdShow("行政部");
        } else if (i.getDepartId() == 22) {
            i.setDepartIdShow("库房部");
        } else if (i.getDepartId() == 23) {
            i.setDepartIdShow("药房部");
        } else if (i.getDepartId() == 24) {
            i.setDepartIdShow("财务部");
        }

        if (i.gettId() == 1) {
            i.settIdShow("前台");
        } else if (i.gettId() == 2) {
            i.settIdShow("医生");
        } else if (i.gettId() == 3) {
            i.settIdShow("收银");
        } else if (i.gettId() == 4) {
            i.settIdShow("药房管理");
        } else if (i.gettId() == 5) {
            i.settIdShow("库房管理");
        } else if (i.gettId() == 6) {
            i.settIdShow("超级管理员");
        }


        if (i != null) {
            map.put("status", "200");
            map.put("msg", true);
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectUserByuLoginName1")
    public Map<String, Object> selectUserByuLoginName1(String uLoginName) {
        Map<String, Object> map = new HashMap();
        User1 i = user1Service.selectUserByuLoginName(uLoginName);
        System.out.println(uLoginName);
        if (i == null) {
            map.put("status", "200");
            map.put("msg", true);

        } else {
            map.put("data",i);
            map.put("msg", false);
        }
        return map;
    }




    //删除缓存
    @RequestMapping("/selectUserMsgFromRedisByuLoginName")
    public Map<String, Object> selectUserMsgFromRedisByuLoginName() {
        Map<String, Object> map = new HashMap();
        Object object = redisUtil.get("LoggedInUsers");
        if (object != null) {
            redisUtil.del("LoggedInUsers");
        }
        if (object == null) {
            map.put("status", "200");
            map.put("msg", true);
        }
        return map;
    }
}
